package com.wasion.pwportable.protocol.p2;

import static com.wasion.core.util.ConvertInXBaseUtil.byte2Hex;

import com.wasion.pwportable.protocol.p2.FrameFieldsLength.LinkAddrLengthEnum;

/**
 * 
 * @ClassName: FixedFrame
 * @Description: IEC101和IEC104的固定帧结构定义
 * @author 高意
 * @date Apr 13, 2016 4:05:37 PM
 * 
 * 启动字符(10H)------1个字节<br>
 * 控制域C------------1个字节<br>
 * 地址域A------------2(2、4、6)个字节<br>
 * 帧校验和CS---------1个字节<br>
 * 结束字符(16H)------1个字节<br>
 */
public class FixedFrame {

	private String startCharacter = "10"; // 起始字符10，1个字节

	private String controlField; // 控制域C，1个字节

	private String addressField; // 地址域A，2个字节

	private String sumCheck; // 帧校验和CS，1个字节

	private String finalCharacter = "16"; // 结束字符，1个字节

	public FixedFrame(byte[] data, LinkAddrLengthEnum len) {

		// 起始字符
		String temp = byte2Hex(data[0]);
		setStartCharacter(temp);

		// 控制域
		temp = byte2Hex(data[1]);
		setControlField(temp);

		int offset = 2;
		// 地址域
		String addr = "";
		switch (len) {
		case LENGTH_LINK_ADDR_2:
			addr = byte2Hex(data[3]) + byte2Hex(data[2]);
			offset += 2;
			break;
		case LENGTH_LINK_ADDR_4:
			addr = byte2Hex(data[5]) + byte2Hex(data[4]) + byte2Hex(data[3])
					+ byte2Hex(data[2]);
			offset += 4;
			break;
		case LENGTH_LINK_ADDR_6:
			addr = byte2Hex(data[7]) + byte2Hex(data[5]) + byte2Hex(data[5])
					+ byte2Hex(data[4]) + byte2Hex(data[3]) + byte2Hex(data[2]);
			offset += 6;
			break;
		}
		setAddressField(addr);

		// 帧校验和
		temp = byte2Hex(data[offset]);
		setSumCheck(temp);
		offset++;

		// 结束字符
		temp = byte2Hex(data[offset]);
		setFinalCharacter(temp);
	}

	public String getStartCharacter() {
		return startCharacter;
	}

	public void setStartCharacter(String startCharacter) {
		this.startCharacter = startCharacter;
	}

	public String getControlField() {
		return controlField;
	}

	public void setControlField(String controlField) {
		this.controlField = controlField;
	}

	public String getAddressField() {
		return addressField;
	}

	public void setAddressField(String addressField) {
		this.addressField = addressField;
	}

	public String getSumCheck() {
		return sumCheck;
	}

	public void setSumCheck(String sumCheck) {
		this.sumCheck = sumCheck;
	}

	public String getFinalCharacter() {
		return finalCharacter;
	}

	public void setFinalCharacter(String finalCharacter) {
		this.finalCharacter = finalCharacter;
	}
}